﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[UpdateBuget] 
	@CodUnitate smallint,@IDJurnal int,@Data datetime,@ArtAl varchar(40),
	@Suma decimal(18,2)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	DECLARE @OldSume decimal(18,2)
    SELECT @OldSume = SUM(Suma) FROM [Buget] WHERE [CodUnitate]=@CodUnitate AND [IDJurnal]=@IDJurnal AND [ArtAl]=@ArtAl AND CAST(CONVERT(varchar(10),[DataInceput],101) AS Datetime) = @Data
	IF @OldSume is Null
		SET @OldSume=0
	INSERT INTO [ESRP].[dbo].[Buget]
           ([Denumire]
           ,[DataInceput]
           ,[MultiAnual]
           ,[Rectificativ]
           ,[Suma]
           ,[IDJurnal]
           ,[ArtAl]
           ,[CodUnitate])
     VALUES
           (CAST(@OldSume AS varchar(50))
           ,@Data
           ,0
           ,0
           ,@Suma-@OldSume
           ,@IDJurnal
           ,@ArtAl
           ,@CodUnitate)
END